package resources.NTree;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import resources.NTree.*;
import java.util.Vector;

public class PruebaArbolN extends Applet {
	boolean isStandalone = false;

	NTree arbol;
	Label mensajeArbol = new Label();
	Label mensajeRamas = new Label();
	List listaRamas = new List();
	Label mensajeNuevoElemento = new Label();
	TextField cajaNuevoElemento = new TextField();
	Button botonAgregar = new Button();
	Label label1 = new Label();
	TextField cajaPadre = new TextField();
	Button botonMostrarRamas = new Button();

	// Construct the applet
	public PruebaArbolN() {
	}

	// Initialize the applet
	public void init() {
		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Component initialization
	private void jbInit() throws Exception {
		mensajeArbol.setBounds(new Rectangle(12, 12, 226, 25));
		mensajeArbol
				.setText("Arbol Base : (a  ( b ( c ) ( t ) ) (1  ( r ) )  ( f ) ) ");
		this.setLayout(null);

		arbol = new NTree("a");
		arbol.addTo("a", "b");
		arbol.addTo("b", "c");
		arbol.addTo("b", "t");

		arbol.addTo("a", "l");
		arbol.addTo("l", "r");

		arbol.addTo("a", "f");

		mensajeRamas.setBounds(new Rectangle(13, 48, 107, 22));
		mensajeRamas.setText("Ramas del Arbol");
		listaRamas.setBounds(new Rectangle(13, 70, 199, 149));
		mensajeNuevoElemento.setBounds(new Rectangle(358, 65, 93, 25));
		mensajeNuevoElemento.setText("Valor Nodo Hijo");
		cajaNuevoElemento.setBounds(new Rectangle(356, 92, 146, 24));
		botonAgregar.setBounds(new Rectangle(216, 122, 134, 26));
		botonAgregar.setName("");
		botonAgregar.setLabel("Agregar Hijo");
		botonAgregar.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(ActionEvent e) {
				botonAgregar_actionPerformed(e);
			}
		});
		label1.setBounds(new Rectangle(216, 70, 101, 19));
		label1.setText("Valor Nodo Padre");
		cajaPadre.setBounds(new Rectangle(217, 92, 131, 24));
		botonMostrarRamas.setBounds(new Rectangle(13, 227, 125, 25));
		botonMostrarRamas.setLabel("Mostrar Ramas");
		botonMostrarRamas
				.addActionListener(new java.awt.event.ActionListener() {

					public void actionPerformed(ActionEvent e) {
						botonMostrarRamas_actionPerformed(e);
					}
				});
		this.add(mensajeArbol, null);
		this.add(mensajeRamas, null);
		this.add(listaRamas, null);
		this.add(botonAgregar, null);
		this.add(label1, null);
		this.add(cajaPadre, null);
		this.add(cajaNuevoElemento, null);
		this.add(mensajeNuevoElemento, null);
		this.add(botonMostrarRamas, null);
	}

	// Get Applet information
	public String getAppletInfo() {
		return "Applet Information";
	}

	// Get parameter info
	public String[][] getParameterInfo() {
		return null;
	}

	// Main method
	public static void main(String[] args) {
		PruebaArbolN applet = new PruebaArbolN();
		applet.isStandalone = true;
		Frame frame = new Frame();
		frame.setTitle("Applet Frame");
		frame.add(applet, BorderLayout.CENTER);
		applet.init();
		applet.start();
		frame.setSize(400, 320);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((d.width - frame.getSize().width) / 2,
				(d.height - frame.getSize().height) / 2);
		frame.setVisible(true);
	}

	void botonAgregar_actionPerformed(ActionEvent e) {
		if ((cajaNuevoElemento.getText().length() != 0)
				& (cajaPadre.getText().length() != 0)) {
			if ((!arbol.contains(cajaNuevoElemento.getText()))
					& (arbol.contains(cajaPadre.getText()))) {
				arbol.addTo(cajaPadre.getText(), cajaNuevoElemento.getText());
				botonMostrarRamas_actionPerformed(e);
			}
		}

	}

	void botonMostrarRamas_actionPerformed(ActionEvent e) {
		SList listaRamasArbol = new SList();
		arbol.getBranchs(listaRamasArbol);
		int j;
		int i;
		listaRamas.removeAll();
		for (i = 1; i <= listaRamasArbol.size(); i++) {
			Vector unaRama = (Vector) listaRamasArbol.elementAt(i);
			String cadena = new String(" ");
			for (j = 0; j < unaRama.size(); j++)
				cadena = cadena + (String) unaRama.elementAt(j) + " - ";
			listaRamas.add(cadena);
		}
	}
}
